BEGIN {
	cntEventName = 0;
	cntDataSet = 0;
	
	currentNodeName = "";
	currentEventName = "";
	
	print "function drawVisualization() {";
	print "var data = new google.visualization.DataTable();";
	print "  data.addColumn('string', 'Node');";
	print "  data.addColumn('number', 'time');";
}

{
	nodeName = $1;
	timeInterval = $2 % 100;
	eventName = $3;

	if (currentNodeName == "") currentNodeName = nodeName;
	if (currentEventName == "") currentEventName = eventName;
	if (currentTimeInterval == 0) currentTimeInterval = timeInterval;
	
	if (eventName != currentEventName) {
		currentEventName = eventName;
		
		# assign number to event
		eventType = -1;
		for (e=0;e<cntEventName;e++) {
			if (EventNames[e] == eventName) {
				eventType = e;
			}
		}
		if (eventType == -1) {
			EventNames[cntEventName] = eventName;
			eventType = cntEventName;
			cntEventName++;
		}
	}
				
	if ((nodeName != currentNodeName) || (timeInterval != currentTimeInterval)) {

		if (cntDataSet == 0) {
			for (e=0;e<cntEventName;e++) {
				print "  data.addColumn('number', '" EventNames[e] "');";
			}
	        print "  data.addRows([";
		}


		# print statistics
		row = "'" currentNodeName "'";
		row = row ", " currentTimeInterval;
		for (e=0;e<cntEventName;e++) {
			row = row ", " cntEvents[e];		
		}		
		print "[" row "],";
		cntDataSet++;

		# reset counters and names
		currentNodeName = nodeName;
		currentTimeInterval = timeInterval;

		for (e=0;e<cntEventName;e++) {
			cntEvents[e] = 0;
		}
		cntEvents[eventType] = 1;

	} else {
		cntEvents[eventType]++;
	}

}

END {
	row = "'" currentNodeName "'";
	row = row ", " currentTimeInterval;
	for (e=0;e<cntEventName;e++) {
		row = row ", " cntEvents[e];		
	}		
	print "[" row "]";
	
print "  ]);";
print "  var motionchart = new google.visualization.MotionChart(";
print "      document.getElementById('visualization'));";
print "  motionchart.draw(data, {'width': 800, 'height': 400});";
print "}";
}